package com.whnk.attendance.service.impl;

import java.util.List;

import com.whnk.attendance.vo.ProjectEmpInfoCheckVo;
import com.whnk.attendance.vo.ProjectEmpInfoListVo;
import com.whnk.attendance.vo.ProjectEmpInfoQueryVo;
import com.whnk.attendance.vo.ProjectEmpInfoUpdateVo;
import com.whnk.common.core.exception.ServiceException;
import com.whnk.common.core.utils.DateUtils;
import com.whnk.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.whnk.attendance.mapper.ProjectEmpInfoMapper;
import com.whnk.attendance.domain.ProjectEmpInfo;
import com.whnk.attendance.service.IProjectEmpInfoService;

/**
 * 项目人员列Service业务层处理
 * 
 * @author whnk
 * @date 2021-11-17
 */
@Service
public class ProjectEmpInfoServiceImpl implements IProjectEmpInfoService 
{
    @Autowired
    private ProjectEmpInfoMapper projectEmpInfoMapper;

    /**
     * 查询项目人员列
     *
     * @param proEmpId 项目人员列主键
     * @return 项目人员列
     */
    @Override
    public ProjectEmpInfo selectProjectEmpInfoByProEmpId(Long proEmpId)
    {
        return projectEmpInfoMapper.selectProjectEmpInfoByProEmpId(proEmpId);
    }

    /**
     * 查询项目人员列列表
     *
     * @param projectEmpInfo 项目人员列
     * @return 项目人员列
     */
    @Override
    public List<ProjectEmpInfo> selectProjectEmpInfoList(ProjectEmpInfo projectEmpInfo)
    {
        return projectEmpInfoMapper.selectProjectEmpInfoList(projectEmpInfo);
    }

    /**
     * 新增项目人员列
     *
     * @param projectEmpInfo 项目人员列
     * @return 结果
     */
    @Override
    public int insertProjectEmpInfo(ProjectEmpInfo projectEmpInfo)
    {
        projectEmpInfo.setCreateTime(DateUtils.getNowDate());
        return projectEmpInfoMapper.insertProjectEmpInfo(projectEmpInfo);
    }

    /**
     * 修改项目人员列
     *
     * @param projectEmpInfo 项目人员列
     * @return 结果
     */
    @Override
    public int updateProjectEmpInfo(ProjectEmpInfo projectEmpInfo)
    {
        projectEmpInfo.setUpdateTime(DateUtils.getNowDate());
        return projectEmpInfoMapper.updateProjectEmpInfo(projectEmpInfo);
    }

    /**
     * 批量删除项目人员列
     *
     * @param proEmpIds 需要删除的项目人员列主键
     * @return 结果
     */
    @Override
    public int deleteProjectEmpInfoByProEmpIds(Long[] proEmpIds)
    {
        return projectEmpInfoMapper.deleteProjectEmpInfoByProEmpIds(proEmpIds);
    }

    /**
     * 删除项目人员列信息
     *
     * @param proEmpId 项目人员列主键
     * @return 结果
     */
    @Override
    public int deleteProjectEmpInfoByProEmpId(Long proEmpId)
    {
        return projectEmpInfoMapper.deleteProjectEmpInfoByProEmpId(proEmpId);
    }

    /**
     * 分页查询项目人员列表
     */
    @Override
    public List<ProjectEmpInfoListVo> selectProjectEmpInfoListVoList(ProjectEmpInfoQueryVo projectEmpInfoQueryVo){
        return projectEmpInfoMapper.selectProjectEmpInfoListVoList(projectEmpInfoQueryVo);
    }

    /**
     * 根据逻辑主键查询项目人员信息
     */
    @Override
    public ProjectEmpInfoListVo selectProjectEmpInfoListVoByProEmpId(long proEmpId){
        return projectEmpInfoMapper.selectProjectEmpInfoListVoByProEmpId(proEmpId);
    }

    /**
     * 根据项目编号查询项目人员信息
     */
    @Override
    public ProjectEmpInfoListVo selectProjectEmpInfoListVoByPER(ProjectEmpInfoCheckVo projectEmpInfoCheckVo){
        return projectEmpInfoMapper.selectProjectEmpInfoListVoByPER(projectEmpInfoCheckVo);
    }

    /**
     * 校验项目编号，身份证号，评级日期是否唯一
     */
    public boolean checkPER(ProjectEmpInfoUpdateVo projectEmpInfoUpdateVo){
        ProjectEmpInfoCheckVo projectEmpInfoCheckVo = new ProjectEmpInfoCheckVo();
        projectEmpInfoCheckVo.setProNum(projectEmpInfoUpdateVo.getProNum());
        projectEmpInfoCheckVo.setEmpId(projectEmpInfoUpdateVo.getEmpId());
        projectEmpInfoCheckVo.setRankTime(projectEmpInfoUpdateVo.getRankTime());
        ProjectEmpInfoListVo info = projectEmpInfoMapper.checkPER(projectEmpInfoCheckVo);
        if(StringUtils.isNotNull(info) && info.getProEmpId() != projectEmpInfoUpdateVo.getProEmpId()){
            return false;
        }
        return true;
    }

    /**
     * 新增项目人员信息
     */
    @Override
    public int insertProjectEmpInfo(ProjectEmpInfoUpdateVo projectEmpInfoUpdateVo){
        ProjectEmpInfo projectEmpInfo = new ProjectEmpInfo();
        projectEmpInfo.setProEmpId(projectEmpInfoUpdateVo.getProEmpId());
        projectEmpInfo.setProNum(projectEmpInfoUpdateVo.getProNum());
        projectEmpInfo.setProName(projectEmpInfoUpdateVo.getProName());
        projectEmpInfo.setEmpId(projectEmpInfoUpdateVo.getEmpId());
        projectEmpInfo.setEmpNm(projectEmpInfoUpdateVo.getEmpNm());
        projectEmpInfo.setRankTime(projectEmpInfoUpdateVo.getRankTime());
        projectEmpInfo.setBankLevelNum(projectEmpInfoUpdateVo.getBankLevelNum());
        projectEmpInfo.setCreateTime(DateUtils.getNowDate());
        return projectEmpInfoMapper.insertProjectEmpInfo(projectEmpInfo);
    }

    /**
     * 修改项目人员信息
     */
    @Override
    public int updateProjectEmpInfo(ProjectEmpInfoUpdateVo projectEmpInfoUpdateVo){
        ProjectEmpInfo projectEmpInfo = new ProjectEmpInfo();
        projectEmpInfo.setProEmpId(projectEmpInfoUpdateVo.getProEmpId());
        projectEmpInfo.setProNum(projectEmpInfoUpdateVo.getProNum());
        projectEmpInfo.setProName(projectEmpInfoUpdateVo.getProName());
        projectEmpInfo.setEmpId(projectEmpInfoUpdateVo.getEmpId());
        projectEmpInfo.setEmpNm(projectEmpInfoUpdateVo.getEmpNm());
        projectEmpInfo.setRankTime(projectEmpInfoUpdateVo.getRankTime());
        projectEmpInfo.setBankLevelNum(projectEmpInfoUpdateVo.getBankLevelNum());
        projectEmpInfo.setUpdateTime(DateUtils.getNowDate());
        return projectEmpInfoMapper.updateProjectEmpInfo(projectEmpInfo);
    }

    /**
     * 导入项目人员列表
     */
    @Override
    public String importData(List<ProjectEmpInfoListVo> projectEmpInfoList, Boolean isUpdateSupport){
        if (StringUtils.isNull(projectEmpInfoList) || projectEmpInfoList.size() == 0)
        {
            throw new ServiceException("导入项目人员不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (ProjectEmpInfoListVo projectEmpInfoListVo : projectEmpInfoList)
        {
            try
            {
                // 验证是否存在这个项目人员信息
                ProjectEmpInfoCheckVo projectEmpInfoCheckVo = new ProjectEmpInfoCheckVo();
                projectEmpInfoCheckVo.setProNum(projectEmpInfoListVo.getProNum());
                projectEmpInfoCheckVo.setEmpId(projectEmpInfoListVo.getEmpId());
                projectEmpInfoCheckVo.setRankTime(projectEmpInfoListVo.getRankTime());
                ProjectEmpInfoListVo ProjectEmp = projectEmpInfoMapper.selectProjectEmpInfoListVoByPER(projectEmpInfoCheckVo);
                if (StringUtils.isNull(ProjectEmp))
                {
                    ProjectEmpInfoUpdateVo projectEmpInfoUpdateVo = new ProjectEmpInfoUpdateVo();
                    projectEmpInfoUpdateVo.setProEmpId(projectEmpInfoListVo.getProEmpId());
                    projectEmpInfoUpdateVo.setProNum(projectEmpInfoListVo.getProNum());
                    projectEmpInfoUpdateVo.setProName(projectEmpInfoListVo.getProName());
                    projectEmpInfoUpdateVo.setEmpId(projectEmpInfoListVo.getEmpId());
                    projectEmpInfoUpdateVo.setEmpNm(projectEmpInfoListVo.getEmpNm());
                    projectEmpInfoUpdateVo.setRankTime(projectEmpInfoListVo.getRankTime());
                    projectEmpInfoUpdateVo.setBankLevelNum(projectEmpInfoListVo.getBankLevelNum());
                    this.insertProjectEmpInfo(projectEmpInfoUpdateVo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、项目人员信息 " + projectEmpInfoListVo.getProEmpId() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    ProjectEmpInfoUpdateVo projectEmpInfoUpdateVo = new ProjectEmpInfoUpdateVo();
                    //为修改对象的项目人员主键赋值
                    projectEmpInfoUpdateVo.setProEmpId(ProjectEmp.getProEmpId());
                    projectEmpInfoUpdateVo.setProNum(projectEmpInfoListVo.getProNum());
                    projectEmpInfoUpdateVo.setProName(projectEmpInfoListVo.getProName());
                    projectEmpInfoUpdateVo.setEmpId(projectEmpInfoListVo.getEmpId());
                    projectEmpInfoUpdateVo.setEmpNm(projectEmpInfoListVo.getEmpNm());
                    projectEmpInfoUpdateVo.setRankTime(projectEmpInfoListVo.getRankTime());
                    projectEmpInfoUpdateVo.setBankLevelNum(projectEmpInfoListVo.getBankLevelNum());
                    this.updateProjectEmpInfo(projectEmpInfoUpdateVo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、项目人员信息 " + projectEmpInfoListVo.getProEmpId() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、项目人员信息 " + projectEmpInfoListVo.getProEmpId() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、项目人员信息 " + projectEmpInfoListVo.getProEmpId() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

}
